// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
	//新增职位
	async addAction(){
		let data = this.post();

		let orid = this.userinfo.departmentid;
		if (data.id*1) {

			if (!data.positionid*1) {

				let chongfu = await this.model('or_position').where({name: data.name, departmentid: orid, id: ['!=', data.id]}).find();

				if (!think.isEmpty(chongfu)) {
					return this.fail('更改的名称重复');
				}
			}

			let res = await this.model('or_position').update(data);
			return res?this.success('更改成功'):this.fail('更改失败');
		}

		data.departmentid = orid;

		if (data.positionid*1) {
			data.floor = 2;
		} else {
			data.floor = 1;
			let repeat = await this.model('or_position').where({name: data.name, floor: data.floor, departmentid: orid}).select();

			if (!think.isEmpty(repeat)) {
				return this.fail('添加名称重复');
			}
		}

		let res = await this.model('or_position').add(data);
		return res?this.success("添加成功"):this.fail('添加失败！');
	}
	async addpositionAction(){
		let id = this.post('id');
		let shopid = this.userinfo.departmentid;
		let res = await this.model('or_position')
				.where({floor:1,departmentid:shopid,id:id})
				.select();

		return this.json(res);
	}

	async addpositionAction(){
		let id = this.post('id');

		let shopid = this.userinfo.departmentid;

		let res = await this.model('or_position')
				.where({floor:1,departmentid:shopid,id:id})
				.select();

		return this.json(res);
	}

	// 新增职称、删除职称
	async editzhichengAction(){
		// 新增职位下的职称
		let data = this.post();
		data.departmentid = this.userinfo.departmentid;
		data.isdisable = 0;
		let res = await this.model('or_position').add(data);
		return res?this.success({info:'添加成功'}):this.fail({info:'添加失败'});
	}

	async zhiweilistAction(){
		// 查询职位
		let zwlist = await this.model('or_position').where({departmentid:this.userinfo.departmentid,floor:1}).select();
		// 查询职称
		let zclist = await this.model('or_position').where({departmentid:this.userinfo.departmentid,floor:2}).select();

		let data = {
			minor : zwlist,
			opts : zclist
		}
		return this.json(data);
	}

	// 删除职位或职称
	async delAction(){
		let id = this.post('id');
		let orid = this.userinfo.departmentid;

		let zws = await this.model('or_position').where({id: id}).find();
		
		if (zws.positionid) {
			let delzc = await this.model('or_position').where({id: id}).delete();
			return delzc?this.success('删除职称成功'):this.fail('删除职称失败');
		} else {
			let delopts = 1;
			let zcs = await this.model('or_position').where({positionid: zws.id, departmentid: orid}).select();
			if (zcs.length) {
				delopts = await this.model('or_position').where({positionid: zws.id, departmentid: orid}).delete();
			}
			let res = await this.model('or_position').where({id: id}).delete();
			if (delopts && res) {
				return this.success('删除职位及职称成功');
			} else {
				return this.fail('删除职位及职称失败');
			}

		}
	}

	// 编辑职位/职称
	async editAction(){
		let data = JSON.parse(this.post('data'));
		let model = this.model('or_position');
		await model.transaction(async () => {
			try {
				let shopid = this.userinfo.departmentid;
				if (data.floor === 1) {
					let zws = await model.where({id:data.id}).update({name:data.name,showmd:data.showmd});
					let zcs = await model.where({departmentid:shopid,positionid:data.id}).update({showmd:data.showmd});
					if (!(zws && zcs)) {
						await model.rollback();
						return this.fail('更改失败');
					}
				}
				await model.commit();
				return this.success('更改成功');
			} catch(e) {
				await model.rollback();
				return this.fail('更改失败');
			}
		})	
	}
}